<?php

namespace app\common\model;

use think\Model;

class Profession extends Model
{
    /**
     * 专业层次
     * @var array[]
     */
    private $layers = [
        ['id' => 1, 'name' => '本科'],
        ['id' => 2, 'name' => '专科'],
    ];

    /**
     * getInfo
     * @param $id
     * @param string $field
     * @return Profession|array|Model|null
     * @throws
     */
    public function getInfo($id, $field = '')
    {
        $where = ['id' => $id];
        return $this->where($where)->field($field)->find();
    }

    /**
     * getValueById
     * @param $id
     * @param $field
     * @return string
     */
    public function getValueById($id, $field): string
    {
        $info = $this->getInfo($id, $field);
        return $info ? $info[$field] : '';
    }

    /**
     * getList
     * @param $where
     * @param string $field
     * @param int $limit
     * @return array
     * @throws
     */
    public function getList($where, $field = '*', $limit = 1000): array
    {
        $list = $this->where($where)->field($field)->order(['sort'=>'desc','id'=>'asc'])->limit($limit)->select();
        return $list ? $list->toArray() : [];
    }

    /**
     * getProfessionDictById
     * @param array $id
     * @return array
     * @throws
     */
    public function getProfessionDictById(array $id): array
    {
        $list = $this->where([['id', 'in', $id]])->select();
        $list = $list ? $list->toArray() : [];
        return array_dict($list, 'id');
    }

    /**
     * getLayerList
     * @return array[]
     */
    public function getLayerList()
    {
        return $this->layers;
    }

    /**
     * getLayerSelectList
     * @return array[]
     */
    public function getLayerSelectList()
    {
        $list = $this->getLayerList();
        array_unshift($list, ['id' => '0', 'name' => '请选择']);
        return $list;
    }
}